package org.itboys.mobile.service.mongo.weixin;


import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoCustomer;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProject;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProjectInfo;
import org.itboys.mongodb.core.MongoDataSource;

import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：众包_微信_转发、关注_service
 */
@Service
public class ZhongBaoWeiXinProjectInfoService extends BaseMongoService<ZhongBaoProjectInfo> {

    @Autowired
    private SundayWeiXinMemberService memberService;
    @Autowired
    private ZhongBaoWeiXinProjectService projectService;
    @Autowired
    private ZhongBaoWeiXinCustomerService customerService;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<ZhongBaoProjectInfo> getEntityClass() {
        return ZhongBaoProjectInfo.class;
    }


    /**
     *增加或删除转发、关注。1，转发，2关注
     * @param projectId
     * @param memberId
     */
    public void save(Long projectId,Long memberId,Integer type){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("memberId",memberId);
        param.put("type",type);
        if(super.count(param)>0){
           // super.delete(param);
        }else{

            SundayMember member=memberService.getById(memberId);
            super.save(new ZhongBaoProjectInfo( projectId,  memberId,  member.getNickName(),  member.getMobile(),  member.getImage(), null,  type,SundayCommonConstants.NO));
            //2017年12月20日，将用户纳入为发布人客户
            ZhongBaoProject project = projectService.getById(projectId);
            customerService.addCustomer(project.getMemberId(),memberId,2);
        }
    }


    /**
     *校验是否转发、、关注
     * @param projectId
     * @param memberId
     */
    public Integer check(Long projectId,Long memberId,Integer type){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("memberId",memberId);
        param.put("type",type);
        if(super.count(param)>0){
            return 1;
        }else{
          return 0;
        }
    }

    /**
     *获取转发、关注
     * @param projectId
     */
    public List<ZhongBaoProjectInfo> select(HttpServletRequest request, Long projectId, Integer type){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        //2017年12月25日，只查询不是系统自动操作的。
        param.put("isSystem",SundayCommonConstants.NO);
       /* if(LongUtil.isNotZreo(memberId)){
            param.put("memberId",memberId);
        }*/
        param.put("type",type);
        return super.pageQuery(request,param).getData();

    }

    /**
     * 获取转发、、关注
     * @param projectId
     * @param type 1,转发2，关注
     * @return
     */
    public List<ZhongBaoProjectInfo> selectNoPage( Long projectId, Integer type){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("type",type);
        //2017年12月25日，只查询不是系统自动操作的。
        param.put("isSystem",SundayCommonConstants.NO);
        return super.list(param);

    }
    /**
     * 通过用户，获取转发、关注的项目ID集合
     * @param memberId
     * @param type
     * @return
     */
    public List<Long> getProjectIds( Long memberId, Integer type){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        param.put("type",type);
        List<ZhongBaoProjectInfo> infos = super.list(param);
        List<Long> projectIds = new ArrayList<>();
        if(ListUtil.isNotNull(infos)){
            for(ZhongBaoProjectInfo info:infos){
                if(LongUtil.isNotZreo(info.getProjectId()))projectIds.add(info.getProjectId());
            }
        }else{
            projectIds.add(0l);
        }

        return projectIds;

    }

}
